package com.hsy.bibc.modules.my.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPPicassoUtil;
import com.hsy.bibc.common.Consts;
import com.hsy.bibc.modules.common.CommonBaseFragment;
import com.hsy.bibc.modules.login.LoginActivity;
import com.hsy.bibc.modules.my.activity.ActivityWebView;
import com.hsy.bibc.modules.my.activity.ShopStockActivity;
import com.hsy.bibc.modules.my.activity.WasteStockActivity;
import com.hsy.bibc.modules.my.gsondata.MyInfoGsonData;
import com.hsy.bibc.modules.order.OrderActivity;
import com.hsy.bibc.utils.CommonUtils;
import com.hsy.bibc.R;
import com.hsy.bibc.modules.my.activity.MyInformationActivity;
import com.hsy.bibc.utils.HttpUtil;
import com.hsy.bibc.utils.VolleyLoadPicture;
import com.tencent.android.tpush.XGPushManager;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hsy on 2016/3/23.
 */
public class FragmentMy extends CommonBaseFragment {

    private LinearLayout leave, my_save, my_order, my_about, my_shop;
    private RelativeLayout info;
    private TextView nickname, score;
    private ImageView avatar;
    private DPHttpUtil.HttpCallback callback = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {

            MyInfoGsonData.Result result = DPGsonUtil.fromJson(response, MyInfoGsonData.Result.class);
            if (result == null) {
                showToast("服务器内部错误!");
                CommonUtils.hideProgressDialog();
                return;
            }
            showUI(result);
            CommonUtils.hideProgressDialog();
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();
        }

        @Override
        public void onMsgError(int what, int type, String msg) {
            CommonUtils.hideProgressDialog();
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        getHttp();
    }

    private void showUI(MyInfoGsonData.Result result) {
        nickname.setText(result.getName());
        score.setText(result.getScore() + "积分");
//        DPPicassoUtil.with(getAttchedActivity()).load(result.getLogo()).into(avatar);
        //用Volley加载图片
        VolleyLoadPicture vlp = new VolleyLoadPicture(getAttchedActivity(), avatar);
        vlp.getmImageLoader().get(result.getLogo(), vlp.getOne_listener());

//        if (result.getSex().equals("1")) {
//            man_image.setVisibility(View.VISIBLE);
//            woman_image.setVisibility(View.GONE);
//        } else {
//            woman_image.setVisibility(View.VISIBLE);
//            man_image.setVisibility(View.GONE);
//        }
    }

    @Override
    protected int getFragmentLayout() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {

        info = F(R.id.info);
        nickname = F(R.id.nickname);
        avatar = F(R.id.avatar);
        leave = F(R.id.leave);
        score = F(R.id.score);

        my_save = F(R.id.my_save);
        my_order = F(R.id.my_order);
        my_about = F(R.id.my_about);
        my_shop = F(R.id.my_shop);

//        my_about.s

        info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyInformationActivity.class);
                startActivity(intent);
            }
        });
        my_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), WasteStockActivity.class);
                startActivity(intent);
            }
        });
        my_shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ShopStockActivity.class);
                startActivity(intent);
            }
        });
        my_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(OrderActivity.class, "no commitOk");
            }
        });
        my_about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ActivityWebView.class);
                intent.putExtra("web_url", "https://www.baidu.com");
                startActivity(intent);
            }
        });
        leave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                CommonUtils.clearToken(getAttchedActivity());
                XGPushManager.registerPush(getAttchedActivity(), "*");
                getAttchedActivity().startActivityWithTaskCleared(LoginActivity.class);

            }

        });

    }

    private void getHttp() {
        CommonUtils.showProgressDialog(getAttchedActivity());
        HttpUtil.httpGet(getAttchedActivity(), 0, Consts.API_GET_MY_INFO + "?id=" + CommonUtils.getUserId(getAttchedActivity()), callback);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onHandleMessage(Message msg) {

    }

    @Override
    public void onFragmentUpdate() {

    }
}
